// 权限控制
import router from '@/router'
// 导入进度条
import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'

import {
  isLogin
} from '@/utils/commen'

router.beforeEach((to, from, next) => {
  Nprogress.start()
  // 需要权限路由
  if (to.path.includes('my')) {
    if (!isLogin()) {
      // 没有token，跳转登录页
      next('/login')
    } else {
      // 有token，放行
      next()
    }
  } else {
    // 不需要权限路由
    if (to.path === '/login' && isLogin()) {
      // 如果登录状态，去登录页，=>跳转首页
      next('/home')
    } else {
      next()
    }
  }
  Nprogress.done()
})

router.afterEach((to, from, next) => {
  Nprogress.done()
  // to and from are both route objects.
})
